//
//  AIGirlTabBarViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/22.
//

import UIKit

class AIGirlTabBarViewController: UITabBarController {

    let pages: [UIViewController] = [AIGirlHomeViewController(), AIGirlDiscoverViewController(), AIGirlMessageViewController(), AIGirlProfileViewController()]
    
    let imageName: [String] = ["home", "discover", "message", "profile"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        AIGirlApplePay.default.checkIncompleteOrder()
        
        self.tabBar.backgroundImage = UIImage()
        self.tabBar.backgroundColor = "#252754".toColor
//        self.delegate = self
        GlobalTool.refreshUserInfo()
        GlobalTool.getBannerList()
        let home = configPages(page: pages[0], name: imageName[0])
        let discover = configPages(page: pages[1], name: imageName[1])
        let message = configPages(page: pages[2], name: imageName[2])
        let me = configPages(page: pages[3], name: imageName[3])
        
        addChild(home)
        addChild(discover)
        addChild(message)
        addChild(me)
        tabBar.shadowImage = UIImage()
        tabBar.backgroundColor = "#252754".toColor
        selectedIndex = 0
        delegate = self
    }

    private func configPages(page: UIViewController, name: String) -> AIGirlNavViewController {
        page.tabBarItem.image = "AIGirl_Tab_\(name)".toImage?.withRenderingMode(.alwaysOriginal)
        page.tabBarItem.selectedImage = "AIGirl_Tab_\(name)_sel".toImage?.withRenderingMode(.alwaysOriginal)
        let nav = AIGirlNavViewController(rootViewController: page)
//        addChild(nav)
        return nav
    }
    
    func toMsgVC(){
        
        selectedIndex = 2
        DispatchQueue.main.async {
            UIApplication.topController()?.tabBarController?.tabBar.isHidden = true
        }
    }
}

extension AIGirlTabBarViewController: UITabBarControllerDelegate {
    //是否可以点击
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    //点击其他四个item 代理方法
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)   {
        if let navigationController = viewController as? UINavigationController,
            let yourViewController = navigationController.topViewController as? AIGirlMessageViewController {
                DispatchQueue.main.async {
                    yourViewController.tabBarController?.tabBar.isHidden = true
                }
        }
    }
}
